package com.cyl.h5.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.h5.service.H5LbsZbjlService;
import com.cyl.manager.lbs.convert.LbsZbjlConvert;
import com.cyl.manager.lbs.domain.LbsZbjl;
import com.cyl.manager.lbs.pojo.query.LbsZbjlQuery;
import com.cyl.manager.lbs.pojo.vo.LbsZbjlH5VO;
import com.cyl.manager.lbs.pojo.vo.LbsZbjlVO;
import com.cyl.manager.lbs.service.LbsZbjlService;
import com.cyl.manager.ums.controller.MemberTagController;
import com.cyl.manager.ums.domain.entity.Member;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.system.service.ISysDictTypeService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/h5/lbs")
@Slf4j
public class H5LbsZbjlController {

    @Autowired
    private H5LbsZbjlService h5LbsZbjlService;
    
    @Autowired
    private LbsZbjlService lbsZbjlService;
    @Autowired
    private LbsZbjlConvert convert;
    @Autowired
    private RedisCache redisCache;
    
    @Autowired
    private ISysDictTypeService dictTypeService;
    
    @ApiOperation("查询直播列表列表")
    @PostMapping("/zbjl/list")
    public ResponseEntity<List<LbsZbjlH5VO>> list(@RequestBody LbsZbjlQuery query) {
    	Member memberdb =  (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        if (!StringUtils.isEmpty(query.getBeginTime()) && !StringUtils.isEmpty(query.getEndTime()) && memberdb.getDeptZbjId()!=null){
        	 query.setDeptZbjId(memberdb.getDeptZbjId());
        	 List<LbsZbjl> list = h5LbsZbjlService.selectList(query);
        	 return ResponseEntity.ok(convert.dosh52vos(list));
        }
        return ResponseEntity.ok(null);
    }
    
    
    
    @ApiOperation("获取直播间信息")
    @PostMapping("/zbjl/getZbjl")
    public ResponseEntity<LbsZbjl> getZbjl(@RequestBody Long zbjlid){
    	Member memberdb =  (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
    	Long deptDpId = memberdb.getDeptDpId();
    	if(deptDpId==null) {
	        throw new RuntimeException("操作失败，请先绑定店长码。");
    	}
    	LbsZbjl lbsZbjl = h5LbsZbjlService.getByZbjlId(zbjlid,memberdb.getId());
    	
    	if(lbsZbjl.getDeptZbjId().intValue() != memberdb.getDeptZbjId()) {
	        throw new RuntimeException("抱歉，您所在的店铺无法观看此直播!");
    	}
    	// 107 已过期  106  异常  103 已结束
    	if("107".equals(lbsZbjl.getZbjzt()) || "106".equals(lbsZbjl.getZbjzt()) || "103".equals(lbsZbjl.getZbjzt()) ) {
    		// throw new RuntimeException("直播已结束");
    	}
    	lbsZbjl.setZbtldz("");
    	lbsZbjl.setZbtlbydz("");
    	lbsZbjl.setZbtlmy("");
    	lbsZbjl.setZbdlmy("");
    	lbsZbjl.setZbtlbydz("");
	    lbsZbjl.setZbtlbymy("");
	    lbsZbjl.setZblldz("");
	    lbsZbjl.setZbllmy("");
	    lbsZbjl.setZbllbydz("");
	    lbsZbjl.setZbllbymy("");
	    
    	String memberKey = "ZBJ_GKSC_"+zbjlid+"_"+memberdb.getId();
    	Long gkkssj =  redisCache.getCacheObject(memberKey);
    	if(gkkssj==null || gkkssj.longValue() == 0L) {
    		gkkssj = System.currentTimeMillis();
            redisCache.setCacheObject(memberKey, gkkssj,60*5,TimeUnit.MINUTES);
    	}
		Map<String, Object>  params = lbsZbjl.getParams();
		params.put("gkkssj", gkkssj);
		lbsZbjl.setParams(params);
    	long zbkssj = DateUtil.parse(lbsZbjl.getJhkssj()).getTime();
    	if(zbkssj > gkkssj) {
  	  	  	params.put("gkkssj", zbkssj);
    		lbsZbjl.setParams(params);
            redisCache.setCacheObject(memberKey, zbkssj,60*5,TimeUnit.MINUTES);
    	}
    	params.put("memberTag", null);
    	try {
    		List<SysDictData> data = dictTypeService.selectDictDataByType(MemberTagController.MEMBER_TAG_ZBJ+lbsZbjl.getDeptZbjId());
        	params.put("memberTag", data);
		} catch (Exception e) {
		}
        return ResponseEntity.ok(lbsZbjl);
    }
    
 
 
    
}
